function AppendCmd(para)
{
	try
	{
		var str = document.frmList.action;
		var idx = str.indexOf("?");
		if(idx>=0)
		{
			if(str.indexOf("cmd=")>=0)
			{
				re = /cmd=(.[^&]*)/
				str = str.replace(re,"cmd="+para);
			}
			else
				str += "&cmd=" + para;
		}
		else
		{
			str = document.location.href = "?cmd=" + para;
		}
		if(para == "back")
		{
			//删除绑定事件
			$("form[name=frmList]").unbind('submit');
			str = document.frmList.SYSTEM_HTTP_REFERER.value;
			
		}
		document.frmList.action = str;
	}
	catch(e)
	{
		alert(e.description);
		return false;
	}
	return true;
}
var cid = 0;
var href = document.location.href;
var pattern = /Model=([0-9]+)/
var match = href.match(pattern);
var Model= match ? match[1] : 0;
function getMaxIdx(idxForm,Model)
{

	try
	{
		var str = "";
		try
		{
			var obj = document.getElementsByTagName('select');
			for(i=0;i<obj.length;i++)
			{
				if(typeof(obj[i].IdxCondition) != 'undefined')
					str = str + "&" + obj[i].name + '=' + obj[i].value;
			}

		}
		catch(e)
		{}
		//alert("index.php?cmd=maxidx&Model="+Model + str);
		var conn = new XHConn();
		conn.connect("index.php?cmd=maxidx&Model="+Model + str,'POST',"",Resset_getMaxIdx(idxForm));
	}
	catch(e)
	{
		alert(e.description);
	}

}
function Resset_getMaxIdx(idxForm)
{
	return function(){
		if(xmlHttp.readyState == 4)
		{
			if(xmlHttp.status == 200)
			{
				var str = xmlHttp.responseText;
				var obj = eval('document.frmList.'+ idxForm);
				obj.value = str;
			}
		}
	}
}
function PrintInfo2()
{
	try
	{
		element = document.getElementById('AthenaManageTable');
		wn = window.open();
		str = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><title>打印</title><link href=\"css/style1.css\" rel=\"stylesheet\" type=\"text/css\"><style type=\"text/css\" media=\"print\">#printhidden{display:none;}.AthenaManageTable{width:100%;	margin:auto;	border-collapse:collapse;	border:1px solid #CCCCCC;}</style></head><body>";
		str += "<table><tr><td Id=\"AthenaManageTable\">";
		str += element.innerHTML;
		str += "</td></tr></table><script language=\"javascript\">window.print();</script></body><html>";
		wn.document.write(str);
		wn.document.close();
		//wn.document.print();
		//wn.close();
	}
	catch(e)
	{
		alert('打印出错');
	}
}
$(document).ready(function(){
	$(".rRight").css("width",$(document).width()-$(".leftBorder").width());
	//左边菜单展开效果
	var idx = GetCookie('menu_idx');
	var current = $("#menu").find("tr[class]").eq(idx);
	
	if($(current).hasClass("menuBoxH")){
		$(current).removeClass("menuBoxH");
		$(current).addClass("menuBox");
	}
	$(".menuNomorl").click(function(){
		var tr = $(this).parent().next();
		if($(tr).hasClass('menuNomorl')) 
			return;
		if($(tr).hasClass('menuBoxH')){
			$(tr).removeClass("menuBoxH");
			$(tr).addClass("menuBox");
			idx = $(tr).index();
			SetCookie('menu_idx',idx);
		}
		else{
			$(tr).removeClass("menuBox");
			$(tr).addClass("menuBoxH");
			SetCookie('menu_idx',0);
		}
	})
	//设置权限
	if($("#right_list").length == 1){
		$("#right_list img").click(function(){
			var ul = $(this).parent().find("ul");
			if($(ul).css("display") == 'block')
				$(ul).hide();
			else
				$(ul).show();
		})
		$("input[type=checkbox]").click(function(){
			var checked = $(this).attr("checked");
			//设置子节点
			var chk = $(this).parent().find("input[type=checkbox]");
			$(chk).attr("checked",checked);
			if(checked){
				//父节点需要选中
				$(this).parent().parent().parent().find("input[type=checkbox]").eq(0).attr("checked",checked);
			}
			else{
				//同辈如果没有选中，则取消父节点
				var li = $(this).parent().siblings("li");
				for(i=0;i<li.length;i++){
					if($(li).eq(i).find("input[type=checkbox]").eq(0).attr("checked"))
						break;
				}
				if(i>=li.length) $(this).parent().parent().parent().find("input[type=checkbox]").eq(0).attr("checked",checked);
			}
		})
	}
	$("form[name=frmList]").submit(function(){
		var str = document.location.href;
		re = /cmd=(.[^&]*)/
		if(re.test(str))
			return CheckForm();
	});
	//超级管理员设置用户隶属公司的部分
	if($("select[type=cid_did]").length == 1){
		cid_did_bind();
	}
	/*
	多选关联选项
	*/
	
	$("select[type=mmselect]").each(function(index,n){
		var obj = $(this);
		var def = $(this).attr("default");
		var key = $(this).attr("key");
		var list = key.split(",");
		var len = list.length;
		for(var i=0;i<len;i++){
			$("select[name="+list[i]+"]").change(function(){
				init_mmselect(obj,list);
			});
		}
		if(index == $("select[type=mmselect]").length-1)init_mmselect(obj,list);
	})
	/*
	关联菜单
	*/
	$("select[type=mselect]").each(function(i,n){
		var obj = $(this);
		var def = $(this).attr("default");
		var key = $(this).attr("key");
		$(this).change(function(){
			init_mselect(obj);
		})
	})
})

function init_mmselect(obj,list){//alert(obj);
	var len = list.length;
	var field = $(obj).attr("name");
	var data = {field:field};
	var def = $(obj).attr("default");
	for(var i=0;i<len;i++){
		data[$("select[name="+list[i]+"]").attr("name")] = $("select[name="+list[i]+"] option").eq($("select[name="+list[i]+"]").attr("selectedIndex")).val();
	}
	///d(data);return;
	var url = document.location.href;
	re = /cmd=(.[^&]*)/
	url = url.replace(re,"cmd=mmselect");
	$.ajax({
		url:url,
		type:'POST',
		data:data,
		dataType:'json',
		success:function(msg){//alert(msg);return;
			while($("select[name="+field+"] option").length > 0)
				$("select[name="+field+"] option:last").remove();
			if(typeof(msg) != 'object') return;
			len = msg.length;
			for(i=0;i<len;i++){
				s = def == msg[i][0] ? "selected" : '';
				$("<option value='"+msg[i][0]+"' "+s+">"+msg[i][1]+"</option>").appendTo($("select[name="+field+"]"));
			}
		}
	})
}

function init_mselect(obj){
	var field = $(obj).attr("name");
	var def = $(obj).attr("default");
	var child = $(obj).attr("key");
	var data = {field:field,val:$(obj).find("option").eq($(obj).attr("selectedIndex")).val(),child:child};

	var url = document.location.href;
	
	var idx = url.indexOf("?");
	if(idx>=0)
	{
		if(url.indexOf("cmd=")>=0)
		{
			re = /cmd=(.[^&]*)/
			url = url.replace(re,"cmd=mselect");
		}
		else
			url += "&cmd=mselect";
	}
	else
	{
		url = document.location.href = "?cmd=mselect";
	}
	$.ajax({
		url:url,
		type:'POST',
		data:data,
		dataType:'json',
		success:function(msg){
			while($("select[name="+child+"] option").length > 0)
				$("select[name="+child+"] option:last").remove();
			if(typeof(msg) != 'object') return;
			len = msg.length;
			for(i=0;i<len;i++){
				s = def == msg[i][0] ? "selected" : '';
				$("<option value='"+msg[i][0]+"' "+s+">"+msg[i][1]+"</option>").appendTo($("select[name="+child+"]"));
			}
		}
	})
}